import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../views/Login/Login"
import Home from "../views/Home/Home"
import Welcome from "../views/Welcome/Welcome"
import User from "../views/User/User"
import Rights from "../views/Power/Rights"
import Roles from "../views/Power/Roles"
import Categories from "../views/Categories/Categories"
import Params from "../views/Params/Params"
import Goods from "../views/Goods/Goods"
import GoodsAdd from "../views/Goods/GoodsAdd"
import Orders from "../views/Orders/Orders"
import Reports from "../views/Reports/Reports"
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: "/login" },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/Welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: User },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Categories },
      { path: '/params', component: Params },
      { path: '/goods', component: Goods },
      { path: '/goods/add', component: GoodsAdd },
      { path: '/orders', component: Orders },
      { path: '/reports', component: Reports },
    ]
  }
  ,


]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 验证是否登录--导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') next();
  const token = window.sessionStorage.getItem("token")
  if (!token) return next('/login');
  next();

})

export default router
